﻿using System;
using System.Web.UI.WebControls;
using Hotel.Vendor.InterfacePlatformManage.Business.HTicketBusiness;
using Hotel.Vendor.InterfacePlatformManage.Entity.HTicketEntity;
using Hotel.Vendor.InterfacePlatformManage.OfflineSite.Page;
using Newtonsoft.Json;
using System.Data;
using System.Collections.Generic;
using HTicket.Entity;

namespace Hotel.Vendor.InterfacePlatformManage.OfflineSite.VenderStaticData
{
    public partial class TaskGridList : HTicketPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string searchCondition = Request.QueryString["searchData"];
            var searchConditionViewModel = JsonConvert.DeserializeObject<TaskSearchConditionEntity>(searchCondition);

            BindGridView(searchConditionViewModel);
        }


        /// <summary>
        /// 绑定到GridView
        /// </summary>
        public void BindGridView(TaskSearchConditionEntity searchConditionEntity)
        {
            this.grdTask.DataSource = GetTaskList(searchConditionEntity);
            SetBindField();
            this.grdTask.DataBind();
        }

        /// <summary>
        /// 获取TaskList数据
        /// </summary>
        public List<HotelInterface_TaskListProcess> GetTaskList(TaskSearchConditionEntity searchConditionEntity)
        {
            TaskSearchConditionEntity taskSearchConditionEntity = new TaskSearchConditionEntity();
            taskSearchConditionEntity.GroupID = this.UserInfo.GroupInfo.GroupID;

            //分页当前索引
            taskSearchConditionEntity.Index = searchConditionEntity.Index;

            //按条件查询
            taskSearchConditionEntity.Eid = searchConditionEntity.Eid;
            taskSearchConditionEntity.AddTime = searchConditionEntity.AddTime;
            taskSearchConditionEntity.HandleMan = searchConditionEntity.HandleMan;
            taskSearchConditionEntity.Improtance = searchConditionEntity.Improtance;
            taskSearchConditionEntity.ProjectName = searchConditionEntity.ProjectName;
            taskSearchConditionEntity.State = MappProcess(searchConditionEntity.State);
            taskSearchConditionEntity.Subject = searchConditionEntity.Subject;
            taskSearchConditionEntity.TaskID = searchConditionEntity.TaskID;


            var listResult = HotelInterfaceTaskListProcessBusiness.GetALLTaskList(taskSearchConditionEntity);
            var recordCount = HotelInterfaceTaskListProcessBusiness.GetALLTaskListCountByGroupID(taskSearchConditionEntity.GroupID);
            var pageSize = new TaskSearchConditionEntity().PageSize;
            this.hiddenPageCount.Value = (recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1).ToString();
            this.hiddenCount.Value = recordCount.ToString();

            return listResult;

        }


        /// <summary>
        /// 转换状态
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public string MappProcess(string process)
        {
            if (string.IsNullOrEmpty(process))
            {
                return null;
            }

            string result = string.Empty;
            switch (process.ToLower())
            {
                case "new":
                    result = "0";
                    break;
                case "处理中":
                    result = "1";
                    break;
                case "已完成":
                    result = "2";
                    break;
                default:
                    result = process;
                    break;
            }

            return result;
        }


        /// <summary>
        /// GridView行绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdTask_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string taskID = e.Row.Cells[0].Text.Trim();
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';");
                e.Row.Style.Add("cursor", "pointer");
                e.Row.Attributes.Add("grid", taskID);

            }
        }

        /// <summary>
        /// 初始化绑定字段
        /// </summary>
        private void SetBindField()
        {
            string flag = string.Empty;
            this.UserInfo.GroupInfo.GridViewSetting.ForEach(item =>
            {
                if (!flag.Contains(item.ColumnField))
                {
                    BoundField field = new BoundField();
                    field.DataField = item.ColumnField;
                    field.HeaderText = item.ColumnName;
                    field.ItemStyle.Width = new Unit(item.ColumnWidth);
                    this.grdTask.Columns.Add(field);

                    flag += item.ColumnField + "|";
                }
            });
        }
    }
}